package com.example.administrator.hqq.adapter.discover;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.example.administrator.hqq.R;
import com.example.administrator.hqq.bean.group.GroupListBean;
import com.example.administrator.hqq.utils.BoldTextUtils;
import com.example.administrator.hqq.utils.transform.GlideCircleTransform;
import com.example.administrator.hqq.utils.transform.SpacesItemDecoration;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @name: HQQ
 * @describe:
 * @author: 康海涛 QQ2541849981
 * @time: 2018/11/4 11:08
 * @remark:
 */
public class ConvenGroupListAdapter extends BaseAdapter {
    private List<GroupListBean.DataBean> list;
    private Context context;
    private OnClickListener onClickListener;

    public ConvenGroupListAdapter(List<GroupListBean.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("ViewHolder")
    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.new_convention_group_list_item, viewGroup, false);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
            viewHolder.memberPortrait.addItemDecoration(new SpacesItemDecoration(0,10));
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        Glide.with(context).load(list.get(i).getUrl()).transform(new CenterCrop(context), new GlideCircleTransform(context)).into(viewHolder.groupImage);
        viewHolder.groupTitle.setText(list.get(i).getTitle());
        BoldTextUtils.BoldText(viewHolder.groupTitle);
        viewHolder.ageNumber.setText(list.get(i).getSame_age_count() + "");
        viewHolder.peopleNumber.setText(list.get(i).getMember_amount() + "");
        viewHolder.locationNumber.setText(list.get(i).getSame_city_count() + "");
        viewHolder.groupIntroduce.setText(list.get(i).getIntroduction());
        LinearLayoutManager ms = new LinearLayoutManager(context);
        ms.setOrientation(LinearLayoutManager.HORIZONTAL);
        GroupMemberPortraitAdapter portraitAdapter = new GroupMemberPortraitAdapter(list.get(i).getMemberPortraitData(), 1, context);
        viewHolder.memberPortrait.setLayoutManager(ms);
        viewHolder.memberPortrait.setAdapter(portraitAdapter);
        if (list.get(i).getSame_age_count()==0)
        {
            viewHolder.locationAuto.setVisibility(View.GONE);
        }else{
            viewHolder.locationAuto.setVisibility(View.VISIBLE);
        }
        if (list.get(i).getSame_city_count()==0)
        {
            viewHolder.ageAuto.setVisibility(View.GONE);
        }else{
            viewHolder.ageAuto.setVisibility(View.VISIBLE);
        }
        if ("FEMALE".equals(list.get(i).getType()))
        {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                viewHolder.groupAuto.setBackground(context.getDrawable(R.drawable.new_group_list_woman_item_background));
                viewHolder.groupListGenderBackground.setVisibility(View.VISIBLE);
                viewHolder.groupListGenderBackground.setSelected(true);
                viewHolder.groupListGenderIma.setSelected(true);
                viewHolder.groupIntroduce.setBackgroundColor(Color.parseColor("#E6DBDD"));
            }
        }else if("MALE".equals(list.get(i).getType())){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                viewHolder.groupListGenderBackground.setVisibility(View.VISIBLE);
                viewHolder.groupAuto.setBackground(context.getDrawable(R.drawable.new_group_list_man_item_background));
                viewHolder.groupListGenderBackground.setSelected(false);
                viewHolder.groupListGenderIma.setSelected(false);
                viewHolder.groupIntroduce.setBackgroundColor(Color.parseColor("#D7E0DE"));
            }
        }else{
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                viewHolder.groupListGenderBackground.setVisibility(View.GONE);
                viewHolder.groupAuto.setBackground(context.getDrawable(R.drawable.new_group_list_item_background));
                viewHolder.groupIntroduce.setBackgroundColor(Color.parseColor("#D7E0DE"));
            }
        }
        //加群点击事件
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onClickListener!=null)
                {
                    onClickListener.joinGroupClick(i);
                }
            }
        });
        return view;
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public interface OnClickListener {
        void informClick(int position);

        void joinGroupClick(int position);
    }

    static class ViewHolder {
        @BindView(R.id.group_image)
        ImageView groupImage;
        @BindView(R.id.group_title)
        TextView groupTitle;
        @BindView(R.id.member_portrait)
        RecyclerView memberPortrait;
        @BindView(R.id.group_title_auto)
        AutoRelativeLayout groupTitleAuto;
        @BindView(R.id.group_introduce)
        TextView groupIntroduce;
        @BindView(R.id.people_number)
        TextView peopleNumber;
        @BindView(R.id.number_auto)
        AutoLinearLayout numberAuto;
        @BindView(R.id.age_number)
        TextView ageNumber;
        @BindView(R.id.age_auto)
        AutoLinearLayout ageAuto;
        @BindView(R.id.location_number)
        TextView locationNumber;
        @BindView(R.id.location_auto)
        AutoLinearLayout locationAuto;
        @BindView(R.id.group_list_gender_ima)
        ImageView groupListGenderIma;
        @BindView(R.id.group_list_gender_background)
        AutoRelativeLayout groupListGenderBackground;
        @BindView(R.id.group_auto)
        AutoRelativeLayout groupAuto;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
